package com.db.williamchart.plugin;

import com.db.williamchart.Grid;
import com.db.williamchart.data.Frame;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PathEffect;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;

import java.util.List;

public class AxisGrid implements Grid {
    private static float DEFAULT_STROKE_WIDTH = 10f;
    private GridType gridType = GridType.FULL;
    private GridEffect gridEffect = GridEffect.SOLID;
    private int color = -0x1000000;
    private Float strokeWidth = DEFAULT_STROKE_WIDTH;

    private Paint paint;


    @Override
    public void draw(Canvas canvas, Frame innerFrame, List<Float> xLabelsPositions, List<Float> yLabelsPositions) {
        if (paint == null){
            paint = new Paint();
            paint.setColor(new Color(this.color));
            paint.setStrokeWidth(strokeWidth);
            if (gridEffect != GridEffect.SOLID){
                float interval = gridEffect == GridEffect.DASHED ? DEFAULT_STROKE_WIDTH * 2 : DEFAULT_STROKE_WIDTH;
                paint.setPathEffect(new PathEffect(new float[]{interval,interval},0));
            }
        }

        if (gridType == GridType.FULL || gridType == GridType.VERTICAL) {

            for (Float i : xLabelsPositions){
                canvas.drawLine(new Point(i, innerFrame.getBottom()), new Point(i, innerFrame.getTop()), paint);
            }

        }

        if (gridType == GridType.FULL || gridType == GridType.HORIZONTAL) {
            for (Float i : yLabelsPositions){
                canvas.drawLine(new Point(innerFrame.getLeft(),i), new Point(innerFrame.getRight(),i), paint);
            }
        }
    }

    public GridType getGridType() {
        return gridType;
    }

    public void setGridType(GridType gridType) {
        this.gridType = gridType;
    }

    public GridEffect getGridEffect() {
        return gridEffect;
    }

    public void setGridEffect(GridEffect gridEffect) {
        this.gridEffect = gridEffect;
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public Float getStrokeWidth() {
        return strokeWidth;
    }

    public void setStrokeWidth(Float strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    public enum  GridType {
        FULL,
        VERTICAL,
        HORIZONTAL
    }

    public enum GridEffect {
        SOLID,
        DASHED,
        DOTTED
    }
}


